package com.project.web.controller.system;

import java.util.List;

import com.project.common.base.TableDataInfo;
import com.project.common.constant.ShiroConstants;
import com.project.system.domain.*;
import com.project.system.service.*;
import com.project.user.domain.UserBrowseRecord;
import com.project.user.domain.UserGoodsPrefer;
import com.project.user.domain.UserGoodsRemark;
import com.project.user.service.*;
import com.project.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.project.common.config.Global;

import javax.servlet.http.HttpServletRequest;

/**
 * 首页 基础业务处理
 *
 * @author
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private IGoodsInfoService goodsInfoService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IUserCartService userCartService;
    @Autowired
    private IUserBrowseRecordService browseRecordService;
    @Autowired
    private IUserGoodsRemarkService goodsRemarkService;
    @Autowired
    private IUserGoodsPreferService goodsPreferService;

    /**
     * 系统首页
     *
     * @param mmap
     * @param request
     * @return
     */
    @GetMapping("/index")
    public String index(ModelMap mmap, HttpServletRequest request) {
        // 取身份信息
        SysUser user = getUser();
//        List<SysDictData> goodsTypes = dictDataService.selectDictDataByType("goods_type_item");
        if (user == null) {
            return "normal/index";
        }
        mmap.put("user", user);
        //后台管理员进入管理后台
        if (user.getUserType().equals("00") || user.getUserType().equals("02")) {
            // 根据用户id取出菜单
            List<SysMenu> menus = menuService.selectMenusByUser(user);
            mmap.put("menus", menus);
            mmap.put("copyrightYear", Global.getCopyrightYear());
            mmap.put("logoText", Global.getLogoText());
            return "system/index";
        }
        //普通用户进入
        request.getSession().setAttribute(ShiroConstants.CURRENT_CART_NUM, userCartService.countUserCart(this.getUserId()));
        return "normal/index";
    }

    /**
     * 查询首页的菜品
     *
     * @param goodsInfo
     * @return
     */
    @RequestMapping("/index/listGoodsByType")
    @ResponseBody
    public List<GoodsInfo> listGoodsByType(GoodsInfo goodsInfo) {
        //首页每个餐饮种类限制显示12条
        goodsInfo.setStatus(0);
        goodsInfo.getParams().put("indexLimit", "12");
        return goodsInfoService.selectGoodsInfoList(goodsInfo);
    }

    /**
     * 系统介绍
     *
     * @param mmap
     * @return
     */
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        mmap.put("version", Global.getVersion());
        return "system/main";
    }

    /**
     * 跳转搜索餐饮
     *
     * @param dictCode
     * @param keyWords
     * @param mmap
     * @return
     */
    @GetMapping("/index/searchGoods")
    public String searchGoods(Long dictCode, String keyWords, ModelMap mmap) {
        SysDictData goodsType = dictDataService.selectDictDataById(dictCode);
        mmap.put("dictCode", dictCode == null ? "" : dictCode);
        mmap.put("goodsType", dictCode == null ? "" : goodsType.getDictValue());
        mmap.put("keyWords", keyWords == null ? "" : keyWords);
        if (goodsType != null) {
            mmap.put("goodsTypeName", goodsType.getDictLabel());
        }

        return "normal/list";
    }

    /**
     * 搜索菜品
     *
     * @param keyWords
     * @param goodsInfo
     * @return
     */
    @PostMapping("/index/searchGoodsList")
    @ResponseBody
    public TableDataInfo searchGoodsList(String keyWords, GoodsInfo goodsInfo) {
        goodsInfo.setStatus(0);
        goodsInfo.getParams().put("keyWords", keyWords);
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 查询菜品评价
     *
     * @param remark
     * @return
     */
    @PostMapping("/index/searchGoodsRemarks")
    @ResponseBody
    public TableDataInfo searchGoodsRemarks(UserGoodsRemark remark) {
        startPage();
        List<UserGoodsRemark> list = goodsRemarkService.selectUserGoodsRemarkList(remark);
        return getDataTable(list);
    }

    /**
     * 跳转到菜品详情页
     *
     * @param goodsId
     * @param mmap
     * @return
     */
    @GetMapping("/index/findGoodsById/{goodsId}")
    public String gooodsDetail(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(goodsId);
        mmap.put("goodsInfo", goodsInfo);
        if (this.getUser() != null) {
            //记录浏览记录
            UserBrowseRecord record = new UserBrowseRecord();
            record.setUserId(this.getUserId());
            record.setGoodsId(goodsId);
            browseRecordService.insertUserBrowseRecord(record);
            mmap.put("preferFlag", goodsPreferService.countUserGoodsPrefer(new UserGoodsPrefer(this.getUserId(), goodsId)).toString());
        } else {
            mmap.put("preferFlag", "0");
        }
        mmap.put("remarksNum", goodsRemarkService.countGoodsRemark(goodsId));
        return "normal/detail";
    }

    @Autowired
    private ISysUserService userService;

    /**
     * 店家中心
     *
     * @param userId
     * @param mmap
     * @return
     */
    @GetMapping("/index/userSeller/{userId}")
    public String userSeller(@PathVariable("userId") Long userId, ModelMap mmap) {
        SysUser seller = userService.selectUserById(userId);
        String phone = seller.getPhonenumber();
        if (phone != null && phone.length() > 0) {
            phone = phone.substring(0, 3) + "****" + phone.substring(7);
            seller.setPhonenumber(phone);
        }
        mmap.put("seller", seller);
        return "normal/user_seller";
    }

    /**
     * 查询店家的菜品
     *
     * @param goodsInfo
     * @return
     */
    @PostMapping("/index/goodsInfo/listForSeller")
    @ResponseBody
    public TableDataInfo listForSeller(GoodsInfo goodsInfo) {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 首页查询公告列表
     *
     * @param notice
     * @return
     */
    @PostMapping("/index/viewList")
    @ResponseBody
    public TableDataInfo viewList(SysNotice notice) {
        notice.setStatus("0");
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新闻详情
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/index/noticeDetail/{id}")
    public String noticeDetail(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("notice", noticeService.selectNoticeById(id));
        return "normal/notice_detail";
    }
}
